package com.dbecom.platform.foundation.batch.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.util.StringUtils;

import com.dbecom.platform.foundation.batch.CatalogBatchErrors;
import com.dbecom.platform.foundation.batch.Constants;
import com.dbecom.platform.foundation.batch.ECBatchException;
import com.dbecom.platform.foundation.batch.model.CatalogGroupDTO;

public class CatalogGroupJobProcessor implements ItemProcessor<CatalogGroupDTO, CatalogGroupDTO>, StepExecutionListener {

    protected static Logger log = LoggerFactory.getLogger(CatalogGroupJobProcessor.class);

    private long jobExecutionId;
    private int lineNumber = Constants.CATALOG_GROUP_HEADER_SKIP_NO;

    @Override
    public CatalogGroupDTO process(CatalogGroupDTO catalogGroupDTO) throws Exception {
        lineNumber++;

        // TODO for below items
        catalogGroupDTO.setCatalogId("1000");
        catalogGroupDTO.setLineNumber(lineNumber);
        catalogGroupDTO.setMemberId("1234567890");
        catalogGroupDTO.setPublished(1);
        catalogGroupDTO.setLanguageId(Constants.LANGUAGE_ID_CN);

        if (StringUtils.isEmpty(catalogGroupDTO.getDelete())) {
            catalogGroupDTO.setDelete(Constants.MARKFORDELETE_NO);
        }

        // GroupIdentifier required and length check

        // TopGroup
        if (!StringUtils.isEmpty(catalogGroupDTO.getTopGroup())
                && "TRUE".equals(catalogGroupDTO.getTopGroup().toUpperCase().trim())) {
            throw new ECBatchException(jobExecutionId, lineNumber, "CatalogGroup.TopGroup",
                    CatalogBatchErrors.TOPGROUP_TYPE_ERROR, "TopGroup should be TRUE or blank.");
        }

        // TopGroup and ParentGroupIdentifier cannot be blank at the same time.
        if (StringUtils.isEmpty(catalogGroupDTO.getTopGroup())
                && StringUtils.isEmpty(catalogGroupDTO.getParentGroupId())) {
            throw new ECBatchException(jobExecutionId, lineNumber, CatalogBatchErrors.TOPGROUP_PARENTGROUP_ERROR,
                    "TopGroup and ParentGroupIdentifier cannot be blank at the same time.");
        }

        // ParentGroupIdentifier length

        // Sequence required check and number check

        // Name required check

        // ShortDescription length

        // LongDescription length

        // Thumbnail length

        // FullImage length

        // Keyword length

        // Delete number check

        return catalogGroupDTO;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.jobExecutionId = stepExecution.getJobExecutionId();
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }

}
